// 取消promise
// Promise本身是不能取消，状态不可以逆，我们借助AbortController来取消Promise,其实就是可以改变Promise状态为reject
function request(signal) {
    return new Promise((resolve, reject) => {
        const asyncTimeout = setTimeout(() => {
            console.log('异步操作');
            resolve(10)
        }, 5000)
        signal.addEventListener('abort', () => {
            console.log('请求被取消了');
            asyncTimeout && clearTimeout(asyncTimeout)
            reject(new DOMException('任务取消了', 'AbortError'))
        })
    })
}

const controller = new AbortController()

request(controller.signal).then((res) => {
    resolve(res)
}).catch(err => {
    if(err.name === 'AbortError') {
        console.log('请求被取消了');
    }
})

setTimeout(() => {
    controller.abort()
}, 2000) 